﻿using System.IO;
using Com.Lancetinc.Evs.Helpers;
using Newtonsoft.Json;

namespace Com.Lancetinc.Evs.Config;

public interface IAppConfig
{
    [JsonIgnore]
    string ConfigPath { get; }
}

public static class AppConfigs
{
    private static ReportConfig _reportConfig;
    public static ReportConfig ReportConfig
    {
        get
        {
            if (_reportConfig == null)
            {
                _reportConfig = ReportConfig.Default;
                _reportConfig.Load();
            }
            return _reportConfig;
        }
    }
}

public static class AppConfigExt
{
    public static void Save(this IAppConfig config)
    {
        JsonConfigUtil.Write(config.ConfigPath, config);
    }

    public static bool Load(this IAppConfig config)
    {
        var jsonFile = config.ConfigPath;
        if (!File.Exists(jsonFile))
        {
            return false;
        }

        var obj = JsonConfigUtil.Read(config.ConfigPath, config.GetType());
        if (obj == null) return false;

        BeanUtil.CopyProperties(obj, config);
        return true;
    }
}
